package com.lineage.server.timecontroller.server;

import com.lineage.server.datatables.LightSpawnTable;
import com.lineage.server.datatables.NpcSpawnTable;
import com.lineage.server.model.Instance.L1FieldObjectInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.gametime.L1GameTimeClock;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerLightTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(ServerLightTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  private static boolean isSpawn = false;
  
  public void start() {
    int timeMillis = 60000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 60000L, 60000L);
  }
  
  public void run() {
    try {
      checkLightTime();
      NpcSpawnTable.get().checkMaps();
    } catch (Exception e) {
      _log.error("照明物件召唤时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      ServerLightTimer lightTimer = new ServerLightTimer();
      lightTimer.start();
    } 
  }
  
  private static void checkLightTime() {
    try {
      int serverTime = L1GameTimeClock.getInstance().currentTime().getSeconds();
      int nowTime = serverTime % 86400;
      if (nowTime >= 21300 && nowTime < 64500) {
        if (isSpawn) {
          isSpawn = false;
          Iterator<L1Object> iterator = World.get().getObject().iterator();
          while (iterator.hasNext()) {
            L1Object object = iterator.next();
            if (object instanceof L1FieldObjectInstance) {
              L1FieldObjectInstance npc = (L1FieldObjectInstance)object;
              if ((npc.getNpcTemplate().get_npcId() != 81177 && npc.getNpcTemplate().get_npcId() != 81178 && 
                npc.getNpcTemplate().get_npcId() != 81179 && 
                npc.getNpcTemplate().get_npcId() != 81180 && 
                npc.getNpcTemplate().get_npcId() != 81181) || (
                npc.getMapId() != 0 && npc.getMapId() != 4))
                continue; 
              npc.deleteMe();
            } 
          } 
        } 
      } else if ((nowTime >= 64500 && nowTime <= 86400) || (
        nowTime >= 0 && nowTime < 21300 && !isSpawn)) {
        isSpawn = true;
        LightSpawnTable.getInstance();
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
